package 回溯;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class _1079_活字印刷 {

    private Integer res = 0;

    private List<String> result = new ArrayList<>();

    public int numTilePossibilities(String tiles) {
        if (tiles == null || tiles.length() == 0) return 0;
        char[] arr = tiles.toCharArray();
        boolean[] used = new boolean[arr.length];
        Arrays.sort(arr);
        backtrack(arr,used,0);
        return res - 1;
    }

    private void backtrack(char[] arr, boolean[] used, int begin) {
        if (begin == arr.length) {
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            if (used[i]) continue;
            if (i > 0 && arr[i - 1] == arr[i] && !used[i - 1]) continue;
            used[i] = true;
        res++;
            backtrack(arr,used,begin + 1);
            used[i] = false;
        }
    }

    public static void main(String[] args) {
        _1079_活字印刷 o = new _1079_活字印刷();
        o.numTilePossibilities("AAB");
        System.out.println(o.result);
    }
}
